import { useHistory } from "react-router";
import { Input, Form, Button, Card, message } from 'antd';
import { useEffect } from 'react';
import { useParams } from 'react-router';
// import AddressApi from '../../api/address';
import { useAddress } from "../../hook/address"

//eslint-disable-next-line
export default () => {
    // 获取路径的动态参数
    const { _id } = useParams();
    const [{ oneAddr }, { amendAdderss, getOneAddress }] = useAddress();

    //数据请求 当依赖发生更新， 数据从新调用
    useEffect(() => {
        //  直接调用hook里面传过来的数据
        getOneAddress(_id)
    }, [_id, getOneAddress])

    const history = useHistory();
    const onFinish = async ({ name, address, phone, Open_hours, longitude, latitude }) => {
        const status = await amendAdderss({ name, address, phone, Open_hours, longitude, latitude, _id })
        // console.log(data);
        // status = data;
        if (status) {
            message.success("添加完成,即将跳转...", 1, () => {
                history.push("/info/address")
            });
        }

    };
    return (
        <div style={{ margin: "30px" }}>

            <Card bordered={true} title="修改自提点" >
                <Form
                    name="basic"
                    labelCol={{ span: 8 }}
                    wrapperCol={{ span: 16 }}
                    onFinish={onFinish}
                    initialValues={oneAddr}
                    autoComplete="off"
                    style={{ width: '500px' }}
                >
                    <Form.Item
                        label="驿站名称"
                        name="name"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item
                        label="驿站地址"
                        name="address"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="手机号码"
                        name="phone"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="营业时间"
                        name="Open_hours"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="经度"
                        name="longitude"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="纬度"
                        name="latitude"
                        rules={[{ required: true, message: '必填项' }]}
                    >
                        <Input />
                    </Form.Item>

                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button type="primary" htmlType="submit">修改提货点</Button>
                    </Form.Item>
                </Form>

            </Card>

        </div >)

}